package com.jecs.business;


import org.wlld.naturalLanguage.word.MyKeyWord;
import org.wlld.naturalLanguage.word.Trust;
import org.wlld.rnnNerveCenter.RandomNerveManager;
import com.jecs.bean.BeanMangerOnly;
import com.jecs.config.Config;
import com.jecs.constant.ErrorCode;
import com.jecs.entity.Response;
import com.jecs.entity.Shop;
import com.jecs.tools.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wlld.naturalLanguage.languageCreator.CatchKeyWord;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @param
 * @DATA
 * @Author LiDaPeng
 * @Description
 */
@Service
public class Business {
    @Autowired
    private BeanMangerOnly beanMangerOnly;

    public Response talk(String word) throws Exception {//对话返回分类
        Response response = new Response();
        if (word != null && word.replace(" ", "").length() > 1) {
            String[] sens = word.replace(" ", "").split("，|。|？|！|；|、|：");
            Map<Integer, CatchKeyWord> catchKeyWordMap = beanMangerOnly.catchKeyWord();//关键词抓取模型
            long eventId = SnowflakeIdWorker.get().nextId();//保证线程安全
            MyKeyWord myKeyWord = beanMangerOnly.getMyKeyWord();
            Shop shop = new Shop();//协议
            RandomNerveManager randomNerveManager = beanMangerOnly.getRandomNerveManager();//语言模型
            int type = 0;
            Set<String> keyWords = null;
            for (int i = 0; i < sens.length; i++) {
                keyWords = null;
                String sen = sens[i];
                Trust trust = randomNerveManager.getType(sen, eventId);
                boolean isKey = myKeyWord.isKeyWord(sen, eventId);//是否有关键词
                if (trust.getTrust() > Config.MinTrust) {//超过最低信任阈值，返回分类
                    type = trust.getKeys().get(0);
                    shop.setFree(isFree(word));
                    shop.setKeyWord(isKey);
                    if (isKey) {
                        CatchKeyWord catchKeyWord = catchKeyWordMap.get(type);
                        if (catchKeyWord != null) {
                            keyWords = catchKeyWord.getKeyWord(sen);
                        }
                        if (keyWords == null || keyWords.size() == 0) {
                            shop.setKeyWord(false);
                        } else {
                            shop.setKeyWord(true);
                        }
                    }
                }
                if (type > 0) {
                    break;
                }
            }
            shop.setSecondType(type);
            shop.setKeyWords(keyWords);
            response.setData(shop);
        } else {
            response.setError(ErrorCode.WordIsNull.getError());
            response.setErrorMessage(ErrorCode.WordIsNull.getErrorMessage());
        }
        return response;
    }

    private boolean isFree(String myWord) {//是否有折扣要求
        boolean isFree = false;
        List<String> freeWords = beanMangerOnly.getFreeWord();
        for (String word : freeWords) {
            if (myWord.contains(word)) {
                isFree = true;
                break;
            }
        }
        return isFree;
    }
}
